<?php
// Napv-ce installer
$noauth = true;
require '../../libraries/master.php';

process_sql('schema.sql');

function process_sql($filename) {
	$sqlbuffer = false;
	$db = Database::getDatabase();
	
	$sql = file($filename);
	foreach ($sql as $line) {
		$line = trim($line);
		if (substr($line, 0 ,5) == '-- !#') {
			// this line is napv-sql code
			if (substr($line, 5, 7) == 'include') {
				$file_to_include = trim(substr($line, 12));
				process_sql($file_to_include);
			}
			elseif (substr($line, 5, 4) == 'quit') {
				return;
			}
		} elseif (substr($line, 0 ,2) == '--') continue;
		$sqlbuffer .= ' '.$line;
		if (substr($sqlbuffer, -1) == ';') {
			$db->query($sqlbuffer);
			$sqlbuffer = '';
		}
	}
}

